<template>
  <div app-container>
    <!-- 数据表 -->
    <el-table
      :data="data"
      style="width: 100%"
      border
      v-loading="listLoading"
      fit
      element-loading-text="加载中"
      highlight-current-row
    >
      <el-table-column prop="index" label="序号" width="60" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="name" label="项目名称" width="150" align="center">
        <template slot-scope="scope">
          <a :href="scope.row.url" target="_blank"> {{ scope.row.name }}</a>
        </template>
      </el-table-column>

      <el-table-column prop="title" label="项目描述" align="center">
        <template slot-scope="scope">
          <p v-for="item in scope.row.description" :key="item">{{ item }}</p>
        </template>
      </el-table-column>

      <el-table-column label="预览图" width="150" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px"
            :src="scope.row.thumb"
            fit="fill"
            :preview-src-list="srcList"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="github"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="success"
              icon="el-icon-paperclip"
              circle
              size="mini"
              @click="openProjectHandle(scope.row)"
            ></el-button>
          </el-tooltip>

          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editProjectHandle(scope.row)"
            ></el-button>
          </el-tooltip>

          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="delProjectHandle(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getProjects, delProject } from "@/api/project";
import { server_url } from "@/urlConfig";
export default {
  data() {
    return { data: [], listLoading: false, srcList: [] };
  },
  created() {
    //创建时先获取所有项目数据
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getProjects().then((res) => {
        this.data = res.data;
        //在地址前面加上服务器地址
        for (var item of this.data) {
          item.thumb = server_url + item.thumb;
          this.srcList.push(item.thumb);
        }
        this.listLoading = false;
      });
    },
    //打开github地址
    openProjectHandle({ github }) {
      window.open(github);
    },
    //删除项目
    delProjectHandle({ id }) {
      this.$confirm("此操作将永久删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delProject(id).then(() => {
            //重新获取数据
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改项目
    editProjectHandle({ id }) {
      this.$router.push(`/editProject/${id}`);
    },
  },
};
</script>

<style></style>
